﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using ZyGames.Framework.Net;
using ZyGames.Framework.Common.Serialization;
using System.Threading;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 房主处理申请筹码结果(同意还是不同意)
	/// 同意的话要给玩家添加上筹码，并且房间玩家筹码记录都要写上
	/// </summary>
	public class Action30053 : BaseAction
	{
		private int tableId;
		private int userId;

		/// <summary>
		/// 是否同意申请筹码
		/// </summary>
		private bool agreeApplyChip;

		private TableData _currentTable;
		private GameUser _currentGameUser;

		public Action30053(HttpGet actionGetter)
			: base((short)ActionType.DZAuditApplyChipResult, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableId", ref tableId) &&
				httpGet.GetInt("UserId", ref userId) &&
				httpGet.GetBool("AgreeApplyChip", ref agreeApplyChip))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			_currentTable = GameRoom.Current.GetTableData(tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息30053获得房间不存在");
				return false;
			}

			_currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30053玩家不存在");
				return false;
			}
			int applyChip;
			if (agreeApplyChip)
			{
				if (!_currentTable.ApplyChipRecore.ContainsKey(userId))
				{
					Tool.WriteErrorLine(string.Format("消息 30053 没有该玩家申请筹码的记录 异常 桌子:{0} UserId:{1}", tableId, userId));
					return false;
				}
				else
				{
					if(_currentTable.ApplyChipRecore.TryGetValue(userId, out applyChip))
					{
						if (applyChip > 0)
						{
							//如果当前玩家是站起状态,添加到leftchip中，如果是坐下状态直接添加到座位的chip
							bool sit = false;
							foreach(var pos in _currentTable.Positions)
							{
								if(pos.UserId == userId)
								{
									if (!_currentTable.AddRealTimeRecord(userId, applyChip, 0))
									{
										Tool.WriteErrorLine(string.Format("消息 30050 当前玩家坐下状态 房主处理玩家申请筹码 异常 桌子:{0} UserId:{1}", tableId, userId));
										return false;
									}
									sit = true;
									pos.Chip += applyChip;
									//同步所有玩家的筹码
									GameTable.Current.SyncAllChip(_currentTable);
									if (_currentTable.State == TableState.Waiting)
										//判断是否已经开局，没有开局则开局
										GameTable.Current.CheckStart(_currentTable);
									break;
								}
							}
							if(!sit) //没有坐下则直接添加到玩家的left筹码中
							{
								if (!_currentTable.AddRealTimeRecord(userId, applyChip, applyChip))
								{
									Tool.WriteErrorLine(string.Format("消息 30050 当前玩家站起状态 房主处理玩家申请筹码 异常 桌子:{0} UserId:{1}", tableId, userId));
									return false;
								}
							}
							
							var parameters = new Parameters();
							parameters["TableId"] = tableId;
							parameters["UserId"] = userId;
							parameters["UserName"] = _currentGameUser.GetNickName();
							parameters["ApplyChip"] = applyChip;
							GameTable.Current.SyncNotifyAction((int)ActionType.DZBrocastAuditApplyChipResult, _currentTable, parameters, t =>
							{
								Tool.WriteLine("广播消息30054 房主响应玩家申请筹码请求");
							});
						}
						else
						{
							Tool.WriteErrorLine(string.Format("消息 30053 该玩家申请筹码的记录为0 异常 桌子:{0} UserId:{1}", tableId, userId));
							_currentTable.ApplyChipRecore.Remove(userId);
							return false;
						}
					}
				}
			}
			else
			{
				//不同意
				
			}
			_currentTable.ApplyChipRecore.Remove(userId);
			return true;
		}
	}
}
